// --------------------------------------------------------------------------------------------------------------------
// <copyright file="RemotingStub.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework
{
    #region Usings

    using System;
    using System.Runtime.Remoting;

    #endregion

    /// <summary>
    /// The interface remoting stub.
    /// </summary>
    public interface IRemotingStub
    {
        #region Public Methods

#if BENCHMARKS
    
    
    
    
    // <summary>
    // The benchmarking1 kb void.
    // </summary>
    // <returns>
    /// </returns>
        byte[] Benchmarking1KBVoid();

        /// <summary>
        /// The benchmarking_ void void.
        /// </summary>
        void BenchmarkingVoidVoid();

        /// <summary>
        /// The ping.
        /// </summary>
        /// <returns>
        /// The ping.
        /// </returns>
        string Ping();
#endif

        #endregion
    }

    /// <summary>
    /// The remoting stub.
    /// </summary>
    public class RemotingStub : MarshalByRefObject, IRemotingStub
    {
        #region Constants and Fields

        /// <summary>
        /// The oneKilobyte.
        /// </summary>
        private readonly byte[] oneKilobyte = new byte[1024];

        #endregion

        #region Public Methods

        /// <summary>
        /// The create obj ref.
        /// </summary>
        /// <param name="requestedType">
        /// The requested type.
        /// </param>
        /// <returns>
        /// </returns>
        public override ObjRef CreateObjRef(Type requestedType)
        {
            var r = base.CreateObjRef(requestedType);

            // Stdout.Write(
            // r.TypeInfo.TypeName + "\n URI                " + r.URI + "\n FromThisProcess    " +
            // r.IsFromThisProcess() + "\n FromThisAppDomain  " + r.IsFromThisAppDomain());

            // foreach (var v in r.ChannelInfo.ChannelData)
            // {
            // if (v is ChannelDataStore)
            // {
            // var i = 0;
            // var ds = v as ChannelDataStore;
            // foreach (var uri in ds.ChannelUris)
            // {
            // Stdout.WriteLine("\n ChannelURIs[{0}]        {1}", i++, uri);
            // }
            // }
            // else
            // {
            // Stdout.WriteLine("\n ChannelData           " + v);
            // }
            // }

            // Stdout.WriteLine();
            return r;
        }

        /// <summary>
        /// The initialize lifetime service.
        /// </summary>
        /// <returns>
        /// The initialize lifetime service.
        /// </returns>
        public override object InitializeLifetimeService()
        {
            var r = base.InitializeLifetimeService();

            // Stdout.WriteLine("" + Descriptor.ID + " Has unlimited lifetime (default was " + r.ToString() + ")");

            // Forever
            return null;
        }

        #endregion

        #region Implemented Interfaces

        #region IRemotingStub

#if BENCHMARKS
    
    
    
    
    // <summary>
    // The benchmarking1 kb void.
    // </summary>
    // <returns>
    /// </returns>
        public byte[] Benchmarking1KBVoid()
        {
            return this.oneKilobyte;
        }

        /// <summary>
        /// The benchmarking_ void void.
        /// </summary>
        public void BenchmarkingVoidVoid()
        {
        }

        /// <summary>
        /// The ping.
        /// </summary>
        /// <returns>
        /// The ping.
        /// </returns>
        public string Ping()
        {
            return "Pong";
        }

#endif

        #endregion

        #endregion
    }
}